home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / TextureMap / Texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  4.5 KB  |  179 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*      TEXTURE.C            */
  3. /****************************/
  4.  
  5.  
  6. /***************/
  7. /* EXTERNALS   */
  8. /***************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DShader.h>
  12. #include <QD3DStorage.h>
  13. #include <qdoffscreen.h>
  14.  
  15.  
  16. /****************************/
  17. /*    PROTOTYPES            */
  18. /****************************/
  19.  
  20. void LoadMyTextureMap(void);
  21. TQ3ShaderObject    GetMyTextureMapFromPICT(long textureRezID);
  22. void CreateMyTexturePixmapData(PicHandle pict, TQ3StoragePixmap *pixmapData);
  23.  
  24. extern    void DoFatalAlert(Str255 s);
  25.  
  26.  
  27. /****************************/
  28. /*    CONSTANTS             */
  29. /****************************/
  30.  
  31.  
  32. /**********************/
  33. /*     VARIABLES      */
  34. /**********************/
  35.  
  36. TQ3ShaderObject    gMyTextureShaderObject;
  37.  
  38.  
  39. /***************** LOAD MY TEXTUREMAP *****************/
  40.  
  41. void LoadMyTextureMap(void)
  42. {
  43.     gMyTextureShaderObject = GetMyTextureMapFromPICT(128);
  44. }
  45.  
  46.  
  47.  
  48. /**************** GET MY TEXTURE MAP FROM PICT ***********************/
  49. //
  50. // Loads a PICT resource and returns a shader object which is
  51. // based on the PICT converted to a texture map.
  52. //
  53. // INPUT: textureRezID = resource ID of texture PICT to get.
  54. //
  55. // OUTPUT: TQ3ShaderObject = shader object for texture map.
  56. //
  57.  
  58. TQ3ShaderObject    GetMyTextureMapFromPICT(long textureRezID)
  59. {
  60. PicHandle            picture;
  61. TQ3StoragePixmap     pixmap;
  62. TQ3TextureObject    texture;
  63. TQ3ShaderObject        shader;
  64.  
  65.  
  66.                 /* LOAD PICT RESOURCE */
  67.                 
  68.     picture = GetPicture(textureRezID);
  69.     if (picture == nil)
  70.         DoFatalAlert("\pUnable to load texture PICT resource");
  71.     
  72.     
  73.             /* SET PIXMAP DATA FOR PICT */
  74.     
  75.     CreateMyTexturePixmapData(picture,&pixmap);
  76.     
  77.     
  78.             /* MAKE NEW PIXMAP TEXTURE OBJECT */
  79.  
  80.     texture = Q3PixmapTexture_New (&pixmap);
  81.     if (texture == nil)
  82.         DoFatalAlert("\pError calling Q3PixmapTexture_New!");
  83.         
  84.     shader = Q3TextureShader_New (texture);
  85.     if (shader == nil)
  86.         DoFatalAlert("\pError calling Q3TextureShader_New!");
  87.  
  88.  
  89.             /* DISPOSE OF UNNEEDED THINGS */
  90.                     
  91.     Q3Object_Dispose (texture);
  92.     Q3Object_Dispose (pixmap.image);    // Disposes of extra reference to storage obj
  93.                                         // from CreateMyTexturePixmapData
  94.  
  95.     ReleaseResource((Handle)picture);
  96.  
  97.     return(shader);    
  98. }
  99.  
  100.  
  101. /******************** CREATE MY TEXTURE PIXMAP DATA ********************/
  102. //
  103. // This routine takes a PICT, draws it into a GWorld, then uses the GWorld
  104. // do define the parameters for a Storage Pixmap object.  The object is not made
  105. // here, but the data is passed back - the calling routine uses it to create
  106. // the TQ3StoragePixmap object.
  107. //
  108. // INPUT    :    pict = PICT resource handle
  109. //
  110. // OUTPUT    :    pixmapData = pointer to TQ3StoragePixmap data structure
  111. //
  112.  
  113. void CreateMyTexturePixmapData(PicHandle pict, TQ3StoragePixmap *pixmapData)
  114. {
  115. Ptr                     pixAddr;
  116. Rect                     rectGW;
  117. GWorldPtr                 pGWorld,oldGW;
  118. PixMapHandle             hPixMap;
  119. OSErr                    myErr;
  120. GDHandle                oldGD;
  121. unsigned long            size,mapSizeX,mapSizeY,rowBytes;
  122. TQ3StorageObject        storageObj;
  123.  
  124.  
  125.  
  126.                 /* CALC SIZE OF PICT */
  127.                         
  128.     mapSizeX =     (**pict).picFrame.right  - (**pict).picFrame.left;
  129.     mapSizeY =     (**pict).picFrame.bottom - (**pict).picFrame.top,
  130.  
  131.  
  132.             /* CREATE A GWORLD TO DRAW PICT */
  133.  
  134.     SetRect(&rectGW, 0, 0, mapSizeX, mapSizeY);                    // set dimensions
  135.     myErr = NewGWorld(&pGWorld, 32, &rectGW, 0, 0, 0L);            // make 32 bit gworld (must be 32bit!)
  136.     if (myErr)
  137.         DoFatalAlert("\pError making texture GWorld!");
  138.  
  139.     
  140.         /* CALC GWORLD'S PIXMAP ADDR & ROWBYTES */
  141.                 
  142.     hPixMap = GetGWorldPixMap(pGWorld);                            
  143.     pixAddr = GetPixBaseAddr(hPixMap);                            // get addr of pixels
  144.     rowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;    // calc rowbytes
  145.  
  146.     
  147.             /* DRAW PICTURE INTO GWORLD */
  148.             
  149.     GetGWorld(&oldGW, &oldGD);                            // save current port
  150.     SetGWorld(pGWorld, nil);                            // set port to GWorld
  151.     LockPixels(hPixMap);                                // lock pixels
  152.     EraseRect(&rectGW);                                    // clear the GWorld
  153.     DrawPicture(pict, &rectGW);                            // draw PICT
  154.     SetGWorld(oldGW, oldGD);                            // restore port 
  155.  
  156.     
  157.             /* CREATE A MEMORY STORAGE OBJECT */
  158.  
  159.     size = rowBytes * mapSizeY;                            // memory = rowBytes * #rows
  160.     storageObj = Q3MemoryStorage_New((unsigned char *)pixAddr,size);    // make new storage object
  161.  
  162.  
  163.             /* SET PIXMAP DATA FIELDS */
  164.                     
  165.     pixmapData->image        = storageObj;                // image = storage object
  166.     pixmapData->width         = mapSizeX;                    // set pixel width
  167.     pixmapData->height        = mapSizeY;                    // set pixel height
  168.     pixmapData->rowBytes     = rowBytes;                    // set rowBytes
  169.     pixmapData->pixelSize     = 32;                        // 32 bit depth
  170.     pixmapData->pixelType    = kQ3PixelTypeRGB32;        // ditto
  171.     pixmapData->bitOrder    = kQ3EndianBig;                // Mac bit & byte order
  172.     pixmapData->byteOrder    = kQ3EndianBig;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179.